<template>
  <div class="main-view">
    <el-container>
      <el-header>
        <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal">
          <el-menu-item index="1" @click="$router.push('/main',() =>{},() =>{})">
            <i class="el-icon-s-home"></i>
            首页
          </el-menu-item>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-custom"></i>管理员选项
            </template>
            <el-menu-item
              index="2-1"
              v-if="!$store.state.isLogin"
              @click="$router.push('/',() =>{},() =>{})"
            >前往登录</el-menu-item>
            <el-menu-item v-else>{{ $store.getters.getLoginAdmin.name }}</el-menu-item>
            <el-menu-item index="2-2" @click="logout">退出账号</el-menu-item>
            <el-menu-item index="2-3" @click="changePassword">修改密码</el-menu-item>
          </el-submenu>
          <el-menu-item index="3" disabled>
            <i class="el-icon-message-solid"></i>消息中心
          </el-menu-item>
          <el-menu-item index="4">
            <i class="el-icon-s-promotion"></i>
            前往用户界面
          </el-menu-item>
          <el-menu-item style="float:right">
            <h3>欢迎使用公租房后台管理系统</h3>
          </el-menu-item>
        </el-menu>
        <div class="line"></div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-radio-group v-model="isCollapse" style="margin-bottom: 20px; margin-top: 20px">
            <el-radio-button :label="false">展开</el-radio-button>
            <el-radio-button :label="true">收起</el-radio-button>
          </el-radio-group>
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
          >
            <el-submenu index="1" :disabled="!isMenuEnabled('房源管理')">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span slot="title">房源管理</span>
              </template>
              <el-menu-item
                @click="$router.push('/area',() =>{},() =>{})"
                index="2"
                :disabled="!isMenuEnabled('区域管理')"
              >区域管理</el-menu-item>
              <el-menu-item
                @click="$router.push('/community',() =>{},() =>{})"
                index="3"
                :disabled="!isMenuEnabled('小区管理')"
              >小区管理</el-menu-item>
              <el-menu-item
                @click="$router.push('/apartment',() =>{},() =>{})"
                index="4"
                :disabled="!isMenuEnabled('套房管理')"
              >套房管理</el-menu-item>
            </el-submenu>
            <el-submenu index="5" :disabled="!isMenuEnabled('资质审核')">
              <template slot="title">
                <i class="el-icon-s-check"></i>
                <span slot="title">资质审核</span>
              </template>
              <el-menu-item
                @click="$router.push('/audit',() =>{},() =>{})"
                index="6"
                :disabled="!isMenuEnabled('租客审核')"
              >租客审核</el-menu-item>
              <el-menu-item
                @click="$router.push('/apartmentAllocation',() =>{},() =>{})"
                index="7"
                :disabled="!isMenuEnabled('房源分配')"
              >房源分配</el-menu-item>
              <el-menu-item
                index="8"
                @click="$router.push('/contract',() =>{},() =>{})"
                :disabled="!isMenuEnabled('合同管理')"
              >合同管理</el-menu-item>
            </el-submenu>
            <el-submenu index="9" :disabled="!isMenuEnabled('后台管理')">
              <template slot="title">
                <i class="el-icon-user-solid"></i>
                <span slot="title">后台管理</span>
              </template>
              <el-menu-item
                @click="$router.push('/member',() =>{},() =>{})"
                index="10"
                :disabled="!isMenuEnabled('人员管理')"
              >人员管理</el-menu-item>
              <el-menu-item
                @click="$router.push('/permissions',() =>{},() =>{})"
                index="11"
                :disabled="!isMenuEnabled('权限管理')"
              >权限管理</el-menu-item>
              <el-menu-item index="12" :disabled="!isMenuEnabled('菜单管理')">菜单管理</el-menu-item>
            </el-submenu>
            <el-menu-item @click="$router.push('/log',() =>{},() =>{})" index="13">
              <i class="el-icon-s-order"></i>
              <span slot="title">日志管理</span>
            </el-menu-item>
            <el-menu-item index="14">
              <i class="el-icon-s-tools"></i>
              <span slot="title">系统设置</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="旧密码" :label-width="formLabelWidth">
          <el-input type="password" v-model="form.oldPassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :label-width="formLabelWidth">
          <el-input type="password" v-model="form.confirmPassword" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatePassword">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      isCollapse: false,
      dialogFormVisible: false,
      form: {
        id: this.$store.state.loginAdmin.id,
        oldPassword: "",
        password: "",
        confirmPassword: ""
      },
      formLabelWidth: "100px",
      activeMenu: "1",
      permissions: []
    };
  },
  mounted() {
    if (this.$store.state.isLogin == false) {
      this.$router.push(
        "/login",
        () => {},
        () => {}
      );
    } else {
      const currentRoute = this.$route.path;
      const newRoute = "/main";
      if (currentRoute !== newRoute) {
        this.$router.push(
          newRoute,
          () => {},
          () => {}
        );
      }
    }
    this.fetchPermissions();
  },
  methods: {
    logout() {
      this.$confirm("是否退出登录?", "提示", {
        type: "warning"
      })
        .then(() => {
          this.$store.commit("setIsLogin", false);
          this.$store.commit("setLoginAdmin", {});
          this.$router.push("/", () => {});
        })
        .catch(() => {});
    },
    changePassword() {
      this.form = {
        id: this.$store.state.loginAdmin.id,
        oldPassword: "",
        password: ""
      };
      this.dialogFormVisible = true;
    },
    updatePassword() {
      if (this.form.password !== this.form.confirmPassword) {
        this.$message.error("两次输入的密码不一致");
        return;
      }
      this.$axios
        .post("admin/updPwd", this.form)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success("修改成功");
            this.dialogFormVisible = false;
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    fetchPermissions() {
      const roleId = this.$store.getters.getLoginAdmin.roleId;
      this.$axios.get(`/roles/${roleId}/permissions`).then(response => {
        this.permissions = response.data.data;
      });
      this.$store.dispatch("fetchPermissions", roleId).then(() => {
        console.log("Permissions fetched and stored in Vuex");
      });
    },
    isMenuEnabled(menuName) {
      return this.permissions.some(permission => permission.name === menuName);
    }
  }
};
</script>
  
  <style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.el-card {
  margin-bottom: 20px;
}

.el-image {
  width: 100%;
  height: 100%;
}

.el-header,
.el-footer {
  background-color: #ffffff;
  color: #ffffff;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #ffffff;
  color: #ffffff;
  text-align: center;
  height: 100vh;
}

.el-main {
  background-color: #ffffff;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style> 